package com.dimeina.dao.impl;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.dimeina.dao.CategoryDAO;
import com.dimeina.util.BaseDAO;
import com.dimeina.vo.Category;

public class CategoryDAOImpl extends BaseDAO implements CategoryDAO {

	// 获取同级别的所有分类
	// 用于“发布界面”用户选择
	public List<Category> findCategoryByPID(int pid) {
		ArrayList<Category> categpryList = new ArrayList<Category>();
		String sql = "SELECT * FROM `category` WHERE `pid` = ?";
		rs = executeQuery(sql, pid);
		try {
			while (rs.next()) {
				Category category = new Category();
				category.setId(rs.getInt("id"));
				category.setName(rs.getString("name"));
				category.setPid(rs.getInt("pid"));
				category.setDepth(rs.getInt("depth"));
				category.setCreateDate(rs.getTimestamp("createDate"));
				categpryList.add(category);
			}
		} catch (SQLException e) {
			System.out.println("获取同级别的所有分类_DAO异常");
			e.printStackTrace();
		} finally{
			closeAll(rs, pstmt, conn);
		}
		return categpryList;
	}

	// 根据深度获取所有同级分类
	public List<Category> findCategoryByDepth(int depth) {
		ArrayList<Category> categpryList = new ArrayList<Category>();
		String sql = "SELECT * FROM `category` WHERE `depth` = ?";
		rs = executeQuery(sql, depth);
		try {
			while (rs.next()) {
				Category category = new Category();
				category.setId(rs.getInt("id"));
				category.setName(rs.getString("name"));
				category.setPid(rs.getInt("pid"));
				category.setDepth(rs.getInt("depth"));
				category.setCreateDate(rs.getTimestamp("createDate"));
				categpryList.add(category);
			}
		} catch (SQLException e) {
			System.out.println("根据深度获取所有同级分类_DAO异常");
			e.printStackTrace();
		} finally{
			closeAll(rs, pstmt, conn);
		}
		return categpryList;
	}

}
